//
//  MainTabBarController.m
//  PigMarket
//
//  Created by Felix on 2019/3/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "MainTabBarController.h"
#import "TNavigationController.h"
@interface MainTabBarController ()

@end

@implementation MainTabBarController

#pragma mark - view life cycle

+ (void)initialize {
    [self renderStyle];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    [HNavigationBarStyle sharedInstance].barTranslucent = YES;
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
+ (void)renderStyle {
    
    UITabBar *tabBar = [UITabBar appearance];
    tabBar.translucent = NO;
    //设置背景颜色
    [tabBar setBarTintColor:[UIColor whiteColor]];
    
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    
    //设置标题默认状态颜色
    NSDictionary<NSString *,id> *stateNormalAttributes = @{NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#2D2D2D"]};
    [tabBarItem setTitleTextAttributes:stateNormalAttributes forState:UIControlStateNormal];
    
    //设置标题选中状态颜色
    NSDictionary<NSString *,id> *stateSelectedAttributes = @{NSForegroundColorAttributeName: [UIColor primaryColor]};
    [tabBarItem setTitleTextAttributes:stateSelectedAttributes forState:UIControlStateSelected];
}
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self addTabBarItems];
}

- (void) initLayout {
    
}

- (void) initAction {
    
}

- (void)initData {
    
}

#pragma mark - delegate

#pragma mark - target action

#pragma mark - private method
- (void)addTabBarItems {
    // AdoptPigViewController,LoginViewController
    UIViewController *adoptPigVC = [self createChildViewController:@"AdoptPigViewController" title:@"领养" image:@"tabbar_adoptpig"];
    [self addChildViewController:adoptPigVC];
    
    UIViewController *meatMarketVC = [self createChildViewController:@"MeatMainListViewController" title:@"肉市" image:@"tabbar_meatmarket"];
    [self addChildViewController:meatMarketVC];
    
    UIViewController *discoveryVC = [self createChildViewController:@"DiscoveryViewController" title:@"发现" image:@"tabbar_discovery"];
    [self addChildViewController:discoveryVC];
    
    UIViewController *meVC = [self createChildViewController:@"MeViewController" title:@"我的" image:@"tabbar_me"];
    [self addChildViewController:meVC];
//    self.viewControllers = @[adoptPigVC,meatMarketVC,discoveryVC,meVC];
}
- (UIViewController *)createChildViewController:(NSString *)childControllerName
                                          title:(NSString *)title
                                          image:(NSString *)image {
    Class class = NSClassFromString(childControllerName);
    UIViewController *childController = [[class alloc] init];
    
    NSString *selectedImage = [NSString stringWithFormat:@"%@_selected", image];
    childController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    if ([childControllerName isEqualToString:@"DiscoveryViewController"]) {
        TNavigationController *navigationController = [[TNavigationController alloc] initWithRootViewController:childController];
        return navigationController;
    } else {
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:childController];
        return navigationController;
    }
}
#pragma mark - getter and setter

- (void)dealloc {
    PMLog(@"MainTabBarController is dealloc");
}

@end
